<template>
  <div class="content">
    <dt-search ref="search" :label-width="100" :xl="5" :xlBtn="4">
      <Button type="primary" v-if="unlocking('0207051001')" @click="onExport">导出</Button>
    </dt-search>

    <dt-grid ref="table" page :onFetch="onFetch" />
    <!--客户详情-->
    <Modal v-model="detailsFlag" title="客户详情" width="80%" :styles="{ top: '5%' }" :footer-hide="true">
      <detail-page v-if="detailsFlag" :detailsParams="detailsParams"></detail-page>
    </Modal>
  </div>
</template>

<script>
  import Column from './opt/Column.js'
  import SearchOpt from './opt/SearchOpt.js'
  import financialMgr from '@/biz/report/financialReport.js'
  import detailPage from '../../../casemanagement/allCustomersManagement/details.vue'
  import moment from 'moment'

  export default {
    components: {
      detailPage
    },
    data() {
      return {
        search: {
          orgIds:[],
          startDate:'',
          housesId:'',
          endDate:'',
          isGrantedBonus:'0',
        },
        detailsFlag: false,
      }
    },
    mounted() {
      this.$refs.table.init(this, new Column())
      this.$refs.search.init(this.$refs.table, new SearchOpt(), {
        orgIds:[],
        startDate: moment(new Date()).format('YYYY-MM') + '-01',
        endDate: moment(new Date()).format('YYYY-MM-DD'),
        isGrantedBonus:'0',
        housesId:''
      })
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        if (data.startDate && data.endDate && !this.validateDate(data.startDate, data.endDate)) {
          return Promise.reject()
        }
        data.isGrantedBonus = data.isGrantedBonus==0?false:data.isGrantedBonus==1?true:null
        this.search = data
        return financialMgr.getPreBonusSummary(data)
      },
      validateDate(startDate, endDate) {
        let y_start = new Date(startDate).getFullYear(),
            y_end = new Date(endDate).getFullYear(),
            m_start = new Date(startDate).getMonth() + 1,
            m_end = new Date(endDate).getMonth() + 1,
            d_start = new Date(startDate).getDate(),
            d_end = new Date(endDate).getDate()
        let month = (y_end - y_start) * 12 + (m_end - m_start)
        if (month > 6 || (month == 6 && d_end > d_start)) {
          this.$Message.error('签约日期跨度最大6个月')
          return false
        }
        return true
      },
      onExport() {
        financialMgr.exportPreBonusSummaryFun({
          ...this.search
        })
      },
      orderDetailFun(row) {
        let orderParams =  {
          examineBtn: false,
          orderStatusName: '签约',
          orderNum: row.contractCode, // 单号
          businessId: row.contractId, // 业务id
          buildingId: row.housesId,
          subscriptionId: row.orderId,
          cusPreFlag:true
        }

        this.linkOrderFun(orderParams)
      },
      // 客户姓名链接到客户资料
      onLinkUser(params) {
        let row = params[0]
        let type = params[1]
        if (row.customerName == '-') {
          return false
        }
        this.detailsParams = {
          customerId: row.customerId, // 手机号唯一编码
          userCustomerId: row.userCustomerId, // 客户信息唯一编码
          buildingId: row.housesId,
          userId: row.counselorId,
          cityCode: row.cityId
        }
        if(type){
          this.detailsParams.flag =  'clue'
        }

        this.detailsFlag = true
      },
    }
  }
</script>

<style lang="scss" scoped>
  .content {
    padding: 20px;
  }
</style>
